home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / msvccompiler.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  18KB  |  639 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.msvccompiler
  5.  
  6. Contains MSVCCompiler, an implementation of the abstract CCompiler class
  7. for the Microsoft Visual Studio.
  8. '''
  9. __revision__ = '$Id: msvccompiler.py 62197 2008-04-07 01:53:39Z mark.hammond $'
  10. import sys
  11. import os
  12. import string
  13. from distutils.errors import DistutilsExecError, DistutilsPlatformError, CompileError, LibError, LinkError
  14. from distutils.ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
  15. from distutils import log
  16. _can_read_reg = 0
  17.  
  18. try:
  19.     import _winreg
  20.     _can_read_reg = 1
  21.     hkey_mod = _winreg
  22.     RegOpenKeyEx = _winreg.OpenKeyEx
  23.     RegEnumKey = _winreg.EnumKey
  24.     RegEnumValue = _winreg.EnumValue
  25.     RegError = _winreg.error
  26. except ImportError:
  27.     
  28.     try:
  29.         import win32api
  30.         import win32con
  31.         _can_read_reg = 1
  32.         hkey_mod = win32con
  33.         RegOpenKeyEx = win32api.RegOpenKeyEx
  34.         RegEnumKey = win32api.RegEnumKey
  35.         RegEnumValue = win32api.RegEnumValue
  36.         RegError = win32api.error
  37.     except ImportError:
  38.         log.info("Warning: Can't read registry to find the necessary compiler setting\nMake sure that Python modules _winreg, win32api or win32con are installed.")
  39.  
  40.  
  41. if _can_read_reg:
  42.     HKEYS = (hkey_mod.HKEY_USERS, hkey_mod.HKEY_CURRENT_USER, hkey_mod.HKEY_LOCAL_MACHINE, hkey_mod.HKEY_CLASSES_ROOT)
  43.  
  44.  
  45. def read_keys(base, key):
  46.     '''Return list of registry keys.'''
  47.     
  48.     try:
  49.         handle = RegOpenKeyEx(base, key)
  50.     except RegError:
  51.         return None
  52.  
  53.     L = []
  54.     i = 0
  55.     while None:
  56.         
  57.         try:
  58.             k = RegEnumKey(handle, i)
  59.         except RegError:
  60.             break
  61.  
  62.         i = i + 1
  63.         continue
  64.         return L
  65.  
  66.  
  67. def read_values(base, key):
  68.     '''Return dict of registry keys and values.
  69.  
  70.     All names are converted to lowercase.
  71.     '''
  72.     
  73.     try:
  74.         handle = RegOpenKeyEx(base, key)
  75.     except RegError:
  76.         return None
  77.  
  78.     d = { }
  79.     i = 0
  80.     while None:
  81.         
  82.         try:
  83.             (name, value, type) = RegEnumValue(handle, i)
  84.         except RegError:
  85.             break
  86.  
  87.         name = name.lower()
  88.         d[convert_mbcs(name)] = convert_mbcs(value)
  89.         i = i + 1
  90.         continue
  91.         return d
  92.  
  93.  
  94. def convert_mbcs(s):
  95.     enc = getattr(s, 'encode', None)
  96.     if enc is not None:
  97.         
  98.         try:
  99.             s = enc('mbcs')
  100.         except UnicodeError:
  101.             pass
  102.         except:
  103.             None<EXCEPTION MATCH>UnicodeError
  104.         
  105.  
  106.     None<EXCEPTION MATCH>UnicodeError
  107.     return s
  108.  
  109.  
  110. class MacroExpander:
  111.     
  112.     def __init__(self, version):
  113.         self.macros = { }
  114.         self.load_macros(version)
  115.  
  116.     
  117.     def set_macro(self, macro, path, key):
  118.         for base in HKEYS:
  119.             d = read_values(base, path)
  120.             if d:
  121.                 self.macros['$(%s)' % macro] = d[key]
  122.                 break
  123.                 continue
  124.         
  125.  
  126.     
  127.     def load_macros(self, version):
  128.         vsbase = 'Software\\Microsoft\\VisualStudio\\%0.1f' % version
  129.         self.set_macro('VCInstallDir', vsbase + '\\Setup\\VC', 'productdir')
  130.         self.set_macro('VSInstallDir', vsbase + '\\Setup\\VS', 'productdir')
  131.         net = 'Software\\Microsoft\\.NETFramework'
  132.         self.set_macro('FrameworkDir', net, 'installroot')
  133.         
  134.         try:
  135.             if version > 7:
  136.                 self.set_macro('FrameworkSDKDir', net, 'sdkinstallrootv1.1')
  137.             else:
  138.                 self.set_macro('FrameworkSDKDir', net, 'sdkinstallroot')
  139.         except KeyError:
  140.             exc = None
  141.             raise DistutilsPlatformError, 'Python was built with Visual Studio 2003;\nextensions must be built with a compiler than can generate compatible binaries.\nVisual Studio 2003 was not found on this system. If you have Cygwin installed,\nyou can try compiling with MingW32, by passing "-c mingw32" to setup.py.'
  142.  
  143.         p = 'Software\\Microsoft\\NET Framework Setup\\Product'
  144.         for base in HKEYS:
  145.             
  146.             try:
  147.                 h = RegOpenKeyEx(base, p)
  148.             except RegError:
  149.                 continue
  150.  
  151.             key = RegEnumKey(h, 0)
  152.             d = read_values(base, '%s\\%s' % (p, key))
  153.             self.macros['$(FrameworkVersion)'] = d['version']
  154.         
  155.  
  156.     
  157.     def sub(self, s):
  158.         for k, v in self.macros.items():
  159.             s = string.replace(s, k, v)
  160.         
  161.         return s
  162.  
  163.  
  164.  
  165. def get_build_version():
  166.     '''Return the version of MSVC that was used to build Python.
  167.  
  168.     For Python 2.3 and up, the version number is included in
  169.     sys.version.  For earlier versions, assume the compiler is MSVC 6.
  170.     '''
  171.     prefix = 'MSC v.'
  172.     i = string.find(sys.version, prefix)
  173.     if i == -1:
  174.         return 6
  175.     i = i + len(prefix)
  176.     (s, rest) = sys.version[i:].split(' ', 1)
  177.     majorVersion = int(s[:-2]) - 6
  178.     minorVersion = int(s[2:3]) / 10
  179.     if majorVersion == 6:
  180.         minorVersion = 0
  181.     
  182.     if majorVersion >= 6:
  183.         return majorVersion + minorVersion
  184.  
  185.  
  186. def get_build_architecture():
  187.     '''Return the processor architecture.
  188.  
  189.     Possible results are "Intel", "Itanium", or "AMD64".
  190.     '''
  191.     prefix = ' bit ('
  192.     i = string.find(sys.version, prefix)
  193.     if i == -1:
  194.         return 'Intel'
  195.     j = string.find(sys.version, ')', i)
  196.     return sys.version[i + len(prefix):j]
  197.  
  198.  
  199. def normalize_and_reduce_paths(paths):
  200.     '''Return a list of normalized paths with duplicates removed.
  201.  
  202.     The current order of paths is maintained.
  203.     '''
  204.     reduced_paths = []
  205.     for p in paths:
  206.         np = os.path.normpath(p)
  207.         if np not in reduced_paths:
  208.             reduced_paths.append(np)
  209.             continue
  210.     
  211.     return reduced_paths
  212.  
  213.  
  214. class MSVCCompiler(CCompiler):
  215.     '''Concrete class that implements an interface to Microsoft Visual C++,
  216.        as defined by the CCompiler abstract class.'''
  217.     compiler_type = 'msvc'
  218.     executables = { }
  219.     _c_extensions = [
  220.         '.c']
  221.     _cpp_extensions = [
  222.         '.cc',
  223.         '.cpp',
  224.         '.cxx']
  225.     _rc_extensions = [
  226.         '.rc']
  227.     _mc_extensions = [
  228.         '.mc']
  229.     src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
  230.     res_extension = '.res'
  231.     obj_extension = '.obj'
  232.     static_lib_extension = '.lib'
  233.     shared_lib_extension = '.dll'
  234.     static_lib_format = shared_lib_format = '%s%s'
  235.     exe_extension = '.exe'
  236.     
  237.     def __init__(self, verbose = 0, dry_run = 0, force = 0):
  238.         CCompiler.__init__(self, verbose, dry_run, force)
  239.         self._MSVCCompiler__version = get_build_version()
  240.         self._MSVCCompiler__arch = get_build_architecture()
  241.         if self._MSVCCompiler__arch == 'Intel':
  242.             if self._MSVCCompiler__version >= 7:
  243.                 self._MSVCCompiler__root = 'Software\\Microsoft\\VisualStudio'
  244.                 self._MSVCCompiler__macros = MacroExpander(self._MSVCCompiler__version)
  245.             else:
  246.                 self._MSVCCompiler__root = 'Software\\Microsoft\\Devstudio'
  247.             self._MSVCCompiler__product = 'Visual Studio version %s' % self._MSVCCompiler__version
  248.         else:
  249.             self._MSVCCompiler__product = 'Microsoft SDK compiler %s' % (self._MSVCCompiler__version + 6)
  250.         self.initialized = False
  251.  
  252.     
  253.     def initialize(self):
  254.         self._MSVCCompiler__paths = []
  255.         if 'DISTUTILS_USE_SDK' in os.environ and 'MSSdk' in os.environ and self.find_exe('cl.exe'):
  256.             self.cc = 'cl.exe'
  257.             self.linker = 'link.exe'
  258.             self.lib = 'lib.exe'
  259.             self.rc = 'rc.exe'
  260.             self.mc = 'mc.exe'
  261.         else:
  262.             self._MSVCCompiler__paths = self.get_msvc_paths('path')
  263.             if len(self._MSVCCompiler__paths) == 0:
  264.                 raise DistutilsPlatformError, "Python was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed." % self._MSVCCompiler__product
  265.             len(self._MSVCCompiler__paths) == 0
  266.             self.cc = self.find_exe('cl.exe')
  267.             self.linker = self.find_exe('link.exe')
  268.             self.lib = self.find_exe('lib.exe')
  269.             self.rc = self.find_exe('rc.exe')
  270.             self.mc = self.find_exe('mc.exe')
  271.             self.set_path_env_var('lib')
  272.             self.set_path_env_var('include')
  273.         
  274.         try:
  275.             for p in string.split(os.environ['path'], ';'):
  276.                 self._MSVCCompiler__paths.append(p)
  277.         except KeyError:
  278.             pass
  279.  
  280.         self._MSVCCompiler__paths = normalize_and_reduce_paths(self._MSVCCompiler__paths)
  281.         os.environ['path'] = string.join(self._MSVCCompiler__paths, ';')
  282.         self.preprocess_options = None
  283.         if self._MSVCCompiler__arch == 'Intel':
  284.             self.compile_options = [
  285.                 '/nologo',
  286.                 '/Ox',
  287.                 '/MD',
  288.                 '/W3',
  289.                 '/GX',
  290.                 '/DNDEBUG']
  291.             self.compile_options_debug = [
  292.                 '/nologo',
  293.                 '/Od',
  294.                 '/MDd',
  295.                 '/W3',
  296.                 '/GX',
  297.                 '/Z7',
  298.                 '/D_DEBUG']
  299.         else:
  300.             self.compile_options = [
  301.                 '/nologo',
  302.                 '/Ox',
  303.                 '/MD',
  304.                 '/W3',
  305.                 '/GS-',
  306.                 '/DNDEBUG']
  307.             self.compile_options_debug = [
  308.                 '/nologo',
  309.                 '/Od',
  310.                 '/MDd',
  311.                 '/W3',
  312.                 '/GS-',
  313.                 '/Z7',
  314.                 '/D_DEBUG']
  315.         self.ldflags_shared = [
  316.             '/DLL',
  317.             '/nologo',
  318.             '/INCREMENTAL:NO']
  319.         if self._MSVCCompiler__version >= 7:
  320.             self.ldflags_shared_debug = [
  321.                 '/DLL',
  322.                 '/nologo',
  323.                 '/INCREMENTAL:no',
  324.                 '/DEBUG']
  325.         else:
  326.             self.ldflags_shared_debug = [
  327.                 '/DLL',
  328.                 '/nologo',
  329.                 '/INCREMENTAL:no',
  330.                 '/pdb:None',
  331.                 '/DEBUG']
  332.         self.ldflags_static = [
  333.             '/nologo']
  334.         self.initialized = True
  335.  
  336.     
  337.     def object_filenames(self, source_filenames, strip_dir = 0, output_dir = ''):
  338.         if output_dir is None:
  339.             output_dir = ''
  340.         
  341.         obj_names = []
  342.         for src_name in source_filenames:
  343.             (base, ext) = os.path.splitext(src_name)
  344.             base = os.path.splitdrive(base)[1]
  345.             base = base[os.path.isabs(base):]
  346.             if ext not in self.src_extensions:
  347.                 raise CompileError("Don't know how to compile %s" % src_name)
  348.             ext not in self.src_extensions
  349.             if strip_dir:
  350.                 base = os.path.basename(base)
  351.             
  352.             if ext in self._rc_extensions:
  353.                 obj_names.append(os.path.join(output_dir, base + self.res_extension))
  354.                 continue
  355.             if ext in self._mc_extensions:
  356.                 obj_names.append(os.path.join(output_dir, base + self.res_extension))
  357.                 continue
  358.             obj_names.append(os.path.join(output_dir, base + self.obj_extension))
  359.         
  360.         return obj_names
  361.  
  362.     
  363.     def compile(self, sources, output_dir = None, macros = None, include_dirs = None, debug = 0, extra_preargs = None, extra_postargs = None, depends = None):
  364.         if not self.initialized:
  365.             self.initialize()
  366.         
  367.         (macros, objects, extra_postargs, pp_opts, build) = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs)
  368.         if not extra_preargs:
  369.             pass
  370.         compile_opts = []
  371.         compile_opts.append('/c')
  372.         if debug:
  373.             compile_opts.extend(self.compile_options_debug)
  374.         else:
  375.             compile_opts.extend(self.compile_options)
  376.         for obj in objects:
  377.             
  378.             try:
  379.                 (src, ext) = build[obj]
  380.             except KeyError:
  381.                 continue
  382.  
  383.             if debug:
  384.                 src = os.path.abspath(src)
  385.             
  386.             if ext in self._c_extensions:
  387.                 input_opt = '/Tc' + src
  388.             elif ext in self._cpp_extensions:
  389.                 input_opt = '/Tp' + src
  390.             elif ext in self._rc_extensions:
  391.                 input_opt = src
  392.                 output_opt = '/fo' + obj
  393.                 
  394.                 try:
  395.                     self.spawn([
  396.                         self.rc] + pp_opts + [
  397.                         output_opt] + [
  398.                         input_opt])
  399.                 continue
  400.                 except DistutilsExecError:
  401.                     msg = None
  402.                     raise CompileError, msg
  403.                     continue
  404.                 
  405.  
  406.             elif ext in self._mc_extensions:
  407.                 h_dir = os.path.dirname(src)
  408.                 rc_dir = os.path.dirname(obj)
  409.                 
  410.                 try:
  411.                     self.spawn([
  412.                         self.mc] + [
  413.                         '-h',
  414.                         h_dir,
  415.                         '-r',
  416.                         rc_dir] + [
  417.                         src])
  418.                     (base, _) = os.path.splitext(os.path.basename(src))
  419.                     rc_file = os.path.join(rc_dir, base + '.rc')
  420.                     self.spawn([
  421.                         self.rc] + [
  422.                         '/fo' + obj] + [
  423.                         rc_file])
  424.                 continue
  425.                 except DistutilsExecError:
  426.                     msg = None
  427.                     raise CompileError, msg
  428.                     continue
  429.                 
  430.  
  431.             else:
  432.                 raise CompileError("Don't know how to compile %s to %s" % (src, obj))
  433.             output_opt = (None<EXCEPTION MATCH>DistutilsExecError) + obj
  434.             
  435.             try:
  436.                 self.spawn([
  437.                     self.cc] + compile_opts + pp_opts + [
  438.                     input_opt,
  439.                     output_opt] + extra_postargs)
  440.             continue
  441.             except DistutilsExecError:
  442.                 msg = None
  443.                 raise CompileError, msg
  444.                 continue
  445.             
  446.  
  447.         
  448.         return objects
  449.  
  450.     
  451.     def create_static_lib(self, objects, output_libname, output_dir = None, debug = 0, target_lang = None):
  452.         if not self.initialized:
  453.             self.initialize()
  454.         
  455.         (objects, output_dir) = self._fix_object_args(objects, output_dir)
  456.         output_filename = self.library_filename(output_libname, output_dir = output_dir)
  457.         if self._need_link(objects, output_filename):
  458.             lib_args = objects + [
  459.                 '/OUT:' + output_filename]
  460.             if debug:
  461.                 pass
  462.             
  463.             
  464.             try:
  465.                 self.spawn([
  466.                     self.lib] + lib_args)
  467.             except DistutilsExecError:
  468.                 msg = None
  469.                 raise LibError, msg
  470.             except:
  471.                 None<EXCEPTION MATCH>DistutilsExecError
  472.             
  473.  
  474.         None<EXCEPTION MATCH>DistutilsExecError
  475.         log.debug('skipping %s (up-to-date)', output_filename)
  476.  
  477.     
  478.     def link(self, target_desc, objects, output_filename, output_dir = None, libraries = None, library_dirs = None, runtime_library_dirs = None, export_symbols = None, debug = 0, extra_preargs = None, extra_postargs = None, build_temp = None, target_lang = None):
  479.         if not self.initialized:
  480.             self.initialize()
  481.         
  482.         (objects, output_dir) = self._fix_object_args(objects, output_dir)
  483.         (libraries, library_dirs, runtime_library_dirs) = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
  484.         if runtime_library_dirs:
  485.             self.warn("I don't know what to do with 'runtime_library_dirs': " + str(runtime_library_dirs))
  486.         
  487.         lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
  488.         if output_dir is not None:
  489.             output_filename = os.path.join(output_dir, output_filename)
  490.         
  491.         if self._need_link(objects, output_filename):
  492.             if target_desc == CCompiler.EXECUTABLE:
  493.                 if debug:
  494.                     ldflags = self.ldflags_shared_debug[1:]
  495.                 else:
  496.                     ldflags = self.ldflags_shared[1:]
  497.             elif debug:
  498.                 ldflags = self.ldflags_shared_debug
  499.             else:
  500.                 ldflags = self.ldflags_shared
  501.             export_opts = []
  502.             for sym in []:
  503.                 export_opts.append('/EXPORT:' + sym)
  504.             ld_args = ldflags + lib_opts + export_opts + objects + [
  505.                 '/OUT:' + output_filename]
  506.             if export_symbols is not None:
  507.                 (dll_name, dll_ext) = os.path.splitext(os.path.basename(output_filename))
  508.                 implib_file = os.path.join(os.path.dirname(objects[0]), self.library_filename(dll_name))
  509.                 ld_args.append('/IMPLIB:' + implib_file)
  510.             
  511.             if extra_preargs:
  512.                 ld_args[:0] = extra_preargs
  513.             
  514.             if extra_postargs:
  515.                 ld_args.extend(extra_postargs)
  516.             
  517.             self.mkpath(os.path.dirname(output_filename))
  518.             
  519.             try:
  520.                 self.spawn([
  521.                     self.linker] + ld_args)
  522.             except DistutilsExecError:
  523.                 msg = None
  524.                 raise LinkError, msg
  525.             except:
  526.                 None<EXCEPTION MATCH>DistutilsExecError
  527.             
  528.  
  529.         None<EXCEPTION MATCH>DistutilsExecError
  530.         log.debug('skipping %s (up-to-date)', output_filename)
  531.  
  532.     
  533.     def library_dir_option(self, dir):
  534.         return '/LIBPATH:' + dir
  535.  
  536.     
  537.     def runtime_library_dir_option(self, dir):
  538.         raise DistutilsPlatformError, "don't know how to set runtime library search path for MSVC++"
  539.  
  540.     
  541.     def library_option(self, lib):
  542.         return self.library_filename(lib)
  543.  
  544.     
  545.     def find_library_file(self, dirs, lib, debug = 0):
  546.         if debug:
  547.             try_names = [
  548.                 lib + '_d',
  549.                 lib]
  550.         else:
  551.             try_names = [
  552.                 lib]
  553.         for dir in dirs:
  554.             for name in try_names:
  555.                 libfile = os.path.join(dir, self.library_filename(name))
  556.                 if os.path.exists(libfile):
  557.                     return libfile
  558.             
  559.         else:
  560.             return None
  561.         return os.path.exists(libfile)
  562.  
  563.     
  564.     def find_exe(self, exe):
  565.         """Return path to an MSVC executable program.
  566.  
  567.         Tries to find the program in several places: first, one of the
  568.         MSVC program search paths from the registry; next, the directories
  569.         in the PATH environment variable.  If any of those work, return an
  570.         absolute path that is known to exist.  If none of them work, just
  571.         return the original program name, 'exe'.
  572.         """
  573.         for p in self._MSVCCompiler__paths:
  574.             fn = os.path.join(os.path.abspath(p), exe)
  575.             if os.path.isfile(fn):
  576.                 return fn
  577.         
  578.         for p in string.split(os.environ['Path'], ';'):
  579.             fn = os.path.join(os.path.abspath(p), exe)
  580.             if os.path.isfile(fn):
  581.                 return fn
  582.         
  583.         return exe
  584.  
  585.     
  586.     def get_msvc_paths(self, path, platform = 'x86'):
  587.         '''Get a list of devstudio directories (include, lib or path).
  588.  
  589.         Return a list of strings.  The list will be empty if unable to
  590.         access the registry or appropriate registry keys not found.
  591.         '''
  592.         if not _can_read_reg:
  593.             return []
  594.         path = path + ' dirs'
  595.         if self._MSVCCompiler__version >= 7:
  596.             key = '%s\\%0.1f\\VC\\VC_OBJECTS_PLATFORM_INFO\\Win32\\Directories' % (self._MSVCCompiler__root, self._MSVCCompiler__version)
  597.         else:
  598.             key = '%s\\6.0\\Build System\\Components\\Platforms\\Win32 (%s)\\Directories' % (self._MSVCCompiler__root, platform)
  599.         for base in HKEYS:
  600.             d = read_values(base, key)
  601.             if d:
  602.                 if self._MSVCCompiler__version >= 7:
  603.                     return string.split(self._MSVCCompiler__macros.sub(d[path]), ';')
  604.                 return string.split(d[path], ';')
  605.             d
  606.         
  607.         if self._MSVCCompiler__version == 6:
  608.             for base in HKEYS:
  609.                 if read_values(base, '%s\\6.0' % self._MSVCCompiler__root) is not None:
  610.                     self.warn('It seems you have Visual Studio 6 installed, but the expected registry settings are not present.\nYou must at least run the Visual Studio GUI once so that these entries are created.')
  611.                     break
  612.                     continue
  613.             
  614.         
  615.         return []
  616.  
  617.     
  618.     def set_path_env_var(self, name):
  619.         """Set environment variable 'name' to an MSVC path type value.
  620.  
  621.         This is equivalent to a SET command prior to execution of spawned
  622.         commands.
  623.         """
  624.         if name == 'lib':
  625.             p = self.get_msvc_paths('library')
  626.         else:
  627.             p = self.get_msvc_paths(name)
  628.         if p:
  629.             os.environ[name] = string.join(p, ';')
  630.         
  631.  
  632.  
  633. if get_build_version() >= 8:
  634.     log.debug('Importing new compiler from distutils.msvc9compiler')
  635.     OldMSVCCompiler = MSVCCompiler
  636.     from distutils.msvc9compiler import MSVCCompiler
  637.     from distutils.msvc9compiler import MacroExpander
  638.  
  639.